home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / stereo / opengl / simple_stereo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.9 KB  |  340 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18.  
  19. #include <Xm/Xm.h>
  20. #include <X11/Intrinsic.h>
  21. #include <X11/extensions/SGIStereo.h>
  22.  
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glx.h>
  26. #include <GL/GLwMDrawA.h>
  27.  
  28. #define Color_Buffer_Size        1
  29. #define Depth_Buffer_Size        1
  30.  
  31. int                    sphere_slices = 16,
  32.                     sphere_stacks = 16,
  33.                     xRot = 0, yRot = 0,
  34.                     prevy = 0, prevx = 0,
  35.                     nan_stereo_flag = 0;
  36.  
  37.  
  38. void                    initCB(Widget, XtPointer, XtPointer),
  39.                     exposeCB(Widget, XtPointer, XtPointer),
  40.                     resizeCB(Widget, XtPointer, XtPointer);
  41.  
  42. void                    SceneDraw(Display *, Window),
  43.                     nanWireSpheref(float, float, float, float),
  44.                     nanSolidSpheref(float, float, float, float);
  45.  
  46. void                     start_rtn(Widget, XEvent*, String*, Cardinal*),
  47.                     rtn(Widget, XEvent*, String*, Cardinal*),
  48.                     end_rtn(Widget, XEvent*, String*, Cardinal*),
  49.                     stereoEnable(Widget, XEvent*, String*, Cardinal*);
  50.  
  51.  
  52.  
  53. main(int argc, char **argv)
  54. {
  55.   XtAppContext        application_context;
  56.   Widget        toplevel, glw;
  57.   Arg            args[10];
  58.   int             n = 0;
  59.   GLXContext        glw_context;
  60.   XtTranslations    trans;
  61.   String         fallback[] = {
  62.                           "*frame*shadowType: SHADOW_IN",
  63.                       "*boiler*width: 400",
  64.                       "*boiler*height: 400",
  65.                       NULL
  66.                       };
  67.   
  68.   char             *molTranslations =
  69.                           "#override\n\
  70.                                          <Btn1Down>:start_rtn() \n\
  71.                                          <Btn1Motion>:rtn() \n\
  72.                                          <Btn1Up>:end_rtn() \n\
  73.                          <Btn2Down>:stereoEnable()\n";
  74.  
  75.   XtActionsRec        actionsTable[] = {
  76.                           {"start_rtn", start_rtn},
  77.                       {"rtn", rtn},
  78.                       {"end_rtn", end_rtn},
  79.                       {"stereoEnable", stereoEnable}
  80.                      };
  81.   
  82.   toplevel = XtAppInitialize(&application_context, "BoilerPlate",
  83.                  (XrmOptionDescList) NULL, 0,
  84.                  &argc, (String *)argv,
  85.                  fallback, (ArgList)NULL, 0);
  86.  
  87.   trans = XtParseTranslationTable(molTranslations);
  88.   XtAppAddActions(application_context, actionsTable, XtNumber(actionsTable));
  89.  
  90.   n=0;
  91.   XtSetArg(args[n], GLwNrgba, TRUE); n++;
  92.   XtSetArg(args[n], GLwNdoublebuffer, TRUE); n++;
  93.   XtSetArg(args[n], GLwNredSize, Color_Buffer_Size); n++;
  94.   XtSetArg(args[n], GLwNgreenSize, Color_Buffer_Size); n++;
  95.   XtSetArg(args[n], GLwNblueSize, Color_Buffer_Size); n++;
  96.   XtSetArg(args[n], GLwNdepthSize, Depth_Buffer_Size); n++;
  97.  
  98.   glw = XtCreateWidget("boiler", glwMDrawingAreaWidgetClass, 
  99.                toplevel, args, n);
  100.   
  101.   XtAddCallback(glw, GLwNginitCallback, initCB, &glw_context);
  102.   XtAddCallback(glw, GLwNexposeCallback, exposeCB, &glw_context);
  103.   XtAddCallback(glw, GLwNresizeCallback, resizeCB, &glw_context);
  104.  
  105.   XtOverrideTranslations(glw, trans);
  106.   XtManageChild(glw);
  107.   
  108.   XtRealizeWidget(toplevel);
  109.   XtAppMainLoop(application_context);
  110. }
  111.  
  112. void 
  113. initCB(Widget w, XtPointer client_data, XtPointer call_data)
  114. {
  115.   Display    *display = XtDisplay(w);
  116.   int        screen = DefaultScreen(display);
  117.   Window    window = XtWindow(w);
  118.   XVisualInfo    *vi;
  119.   GLXContext    *glw_context = (GLXContext *) client_data;
  120.   Arg        args[10];
  121.   float        mat_specular[] = { .72, .8, .93, 1.0 };
  122.  
  123.   XtSetArg(args[0], GLwNvisualInfo, &vi);
  124.   XtGetValues(w, args, 1);
  125.   
  126.   *glw_context = glXCreateContext(display, vi, None, GL_TRUE);
  127.   glXMakeCurrent(display, window, *glw_context);
  128.  
  129.   glMatrixMode(GL_PROJECTION);
  130.   glLoadIdentity();
  131.   gluPerspective(60.0,  1.0,  .25,  15.0);
  132.   glMatrixMode(GL_MODELVIEW);
  133.   glLoadIdentity();
  134.   glTranslatef(0.0, 0.0, -6.0);
  135.  
  136.   glDepthFunc(GL_LEQUAL);
  137.   glEnable(GL_DEPTH_TEST);
  138.   
  139.   glClearColor(0.0, 0.0, 0.0, 1.0);
  140.   glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  141.   glXSwapBuffers(display, window);
  142.   glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  143.  
  144.   glEnable(GL_LINE_SMOOTH);
  145.   glEnable(GL_BLEND);
  146.   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  147.   glLineWidth(1.5);
  148.  
  149.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  150.   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 128.0);
  151.   
  152.   glEnable(GL_LIGHT0);
  153.   glEnable(GL_LIGHTING);
  154.   glEnable(GL_NORMALIZE);
  155.  
  156.   SceneDraw(display, window);
  157. }
  158.  
  159. void
  160. exposeCB(Widget w, XtPointer client_data, XtPointer call_data)
  161. {
  162.   Display       *display = XtDisplay(w);
  163.   Window    window = XtWindow(w);
  164.   GLXContext    *glw_context = (GLXContext *) client_data;
  165.  
  166.   glXMakeCurrent(display, window, *glw_context);
  167.   SceneDraw(display, window);
  168. }
  169.  
  170. void
  171. resizeCB(Widget w, XtPointer client_data, XtPointer call_data)
  172. {
  173.   Display       *display = XtDisplay(w);
  174.   Window        window = XtWindow(w);
  175.   GLXContext    *glw_context = (GLXContext *) client_data;
  176.   GLwDrawingAreaCallbackStruct *Call_Data = 
  177.                        (GLwDrawingAreaCallbackStruct *) call_data;
  178.   
  179.   glXMakeCurrent(display, window, *glw_context);
  180.   glViewport(0, 0,
  181.          (GLuint) Call_Data->width-1,
  182.          (GLuint) Call_Data->height-1);
  183.   
  184.   SceneDraw(display, window);
  185. }
  186.  
  187.  
  188. void SceneDraw(Display *d, Window w)
  189. {
  190.   
  191.   glPushMatrix();
  192.   glRotatef(2.0, 0.0, 1.0, 0.0);
  193.   XSGISetStereoBuffer(d, w, STEREO_BUFFER_LEFT);
  194.   XSync(d, False);
  195.   glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  196.  
  197.   glColor3f(1.0, 1.0, 0.0);
  198.   nanWireSpheref(0.0, 0.0, -1.0, 1.0);
  199.  
  200.   glColor3f(.5, .5, 5.);
  201.   nanSolidSpheref(0.0, 0.0, 1.0, 1.0);
  202.  
  203.   glColor3f(1.0, 0.0, 0.0);
  204.   nanSolidSpheref(-1.0, 0.0, 0.0, 1.0);
  205.  
  206.   glColor3f(0.0, 1.0, 0.0);
  207.   nanSolidSpheref(1.0, 0.0, 0.0, 1.0);
  208.  
  209.   glColor3f(0.0, 0.0, 1.0);
  210.   nanWireSpheref(0.0, 1.0, 0.0, 1.0);
  211.  
  212.   glColor3f(1.0, 0.0, 1.0);
  213.   nanWireSpheref(0.0, -1.0, 0.0, 1.0);
  214.   glPopMatrix();
  215.  
  216.   if(nan_stereo_flag)
  217.     {
  218.       glPushMatrix();
  219.       glRotatef(-2.0, 0.0, 1.0, 0.0);
  220.       XSGISetStereoBuffer(d, w, STEREO_BUFFER_RIGHT);
  221.       XSync(d, False);
  222.       glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  223.  
  224.       glColor3f(1.0, 1.0, 0.0);
  225.       nanWireSpheref(0.0, 0.0, -1.0, 1.0);
  226.  
  227.       glColor3f(.5, .5, 5.);
  228.       nanSolidSpheref(0.0, 0.0, 1.0, 1.0);
  229.  
  230.       glColor3f(1.0, 0.0, 0.0);
  231.       nanSolidSpheref(-1.0, 0.0, 0.0, 1.0);
  232.  
  233.       glColor3f(0.0, 1.0, 0.0);
  234.       nanSolidSpheref(1.0, 0.0, 0.0, 1.0);
  235.  
  236.       glColor3f(0.0, 0.0, 1.0);
  237.       nanWireSpheref(0.0, 1.0, 0.0, 1.0);
  238.  
  239.       glColor3f(1.0, 0.0, 1.0);
  240.       nanWireSpheref(0.0, -1.0, 0.0, 1.0);
  241.       glPopMatrix();
  242.     }
  243.     
  244.   glFlush();
  245.   glXSwapBuffers(d, w);
  246. }
  247.  
  248. void nanSolidSpheref(float x, float y, float z, float radius)
  249. {
  250.   static GLUquadricObj  *quadObj;
  251.   static int            entry = 0;
  252.  
  253.   glEnable(GL_COLOR_MATERIAL);
  254.   glPushMatrix();
  255.   glTranslatef(x, y, z);
  256.   if (!entry)
  257.     {
  258.       quadObj = gluNewQuadric ();
  259.       gluQuadricDrawStyle (quadObj, GLU_FILL);
  260.       gluQuadricOrientation(quadObj, GLU_OUTSIDE);
  261.       gluQuadricNormals (quadObj, GLU_SMOOTH);
  262.     }
  263.   gluSphere (quadObj, radius, sphere_slices, sphere_stacks);
  264.   glPopMatrix();
  265.   glDisable(GL_COLOR_MATERIAL);
  266. }
  267.  
  268. void nanWireSpheref(float x, float y, float z, float radius)
  269. {
  270.   static GLUquadricObj  *quadObj;
  271.   static int            entry = 0;
  272.  
  273.   glEnable(GL_COLOR_MATERIAL);
  274.   glPushMatrix();
  275.   glTranslatef(x, y, z);
  276.   if (!entry)
  277.     {
  278.       quadObj = gluNewQuadric ();
  279.       gluQuadricDrawStyle (quadObj, GLU_LINE);
  280.       gluQuadricOrientation(quadObj, GLU_OUTSIDE);
  281.       gluQuadricNormals (quadObj, GLU_SMOOTH);
  282.     }
  283.   gluSphere (quadObj, radius, sphere_slices, sphere_stacks);
  284.   glPopMatrix();
  285.   glDisable(GL_COLOR_MATERIAL);
  286. }
  287.  
  288. void start_rtn(Widget w, XEvent *event, String *s, Cardinal *c)
  289. {
  290.   prevx = event->xbutton.x;
  291.   prevy = event->xbutton.y;
  292. }
  293.  
  294. void rtn(Widget w, XEvent *event, String *s, Cardinal *c)
  295. {
  296.   int x = event->xbutton.x;
  297.   int y = event->xbutton.y;
  298.  
  299.   yRot +=  5 * (x - prevx);
  300.   xRot +=  5 * (y - prevy);
  301.   prevx = x;
  302.   prevy = y;
  303.   glPushMatrix();
  304.   glRotatef(xRot*0.1, 1.0, 0.0, 0.0);
  305.   glRotatef(yRot*0.1, 0.0, 1.0, 0.0);
  306.   SceneDraw(XtDisplay(w), XtWindow(w));
  307.   glPopMatrix();
  308. }
  309.  
  310. void
  311. end_rtn(Widget w, XEvent *event, String *s, Cardinal *c)
  312. {
  313.   glPushMatrix();
  314.   glRotatef(xRot*0.1, 1.0, 0.0, 0.0);
  315.   glRotatef(yRot*0.1, 0.0, 1.0, 0.0);
  316.   SceneDraw(XtDisplay(w), XtWindow(w));
  317.   glPopMatrix();
  318. }    
  319.  
  320. void
  321. stereoEnable(Widget w, XEvent *event, String *s, Cardinal *c)
  322. {
  323.   static    mode = 0;
  324.  
  325.   nan_stereo_flag = !nan_stereo_flag;
  326.   if(nan_stereo_flag)
  327.     {
  328.       system("/usr/gfx/setmon STR_BOT");
  329.     }
  330.   else
  331.     {
  332.       system("/usr/gfx/setmon 72HZ");
  333.     }
  334.   glPushMatrix();
  335.   glRotatef(xRot*0.1, 1.0, 0.0, 0.0);
  336.   glRotatef(yRot*0.1, 0.0, 1.0, 0.0);
  337.   SceneDraw(XtDisplay(w), XtWindow(w));
  338.   glPopMatrix();
  339. }
  340.